package com.nuoniu.sibanyun.controller.po;


import com.nuoniu.sibanyun.common.BaseController;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.bo.*;
import com.nuoniu.sibanyun.entity.po.VPurchaseArrivalDetail;
import com.nuoniu.sibanyun.entity.po.VPurchaseArrivalMain;
import com.nuoniu.sibanyun.entity.po.VPurchaseInvoiceMain;
import com.nuoniu.sibanyun.service.erp.IErpVoucherDesignService;
import com.nuoniu.sibanyun.service.po.IVPurchaseArrivalDetailService;
import com.nuoniu.sibanyun.service.po.IVPurchaseArrivalMainService;
import com.nuoniu.sibanyun.service.po.IVPurchaseInvoiceMainService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 采购到货单管理
 * @author 小啊刚
 * @since 2020-11-19
 */
@RestController
@RequestMapping("/po/purchaseArrival")
@Api(value="采购到货单管理", tags={"采购到货单管理"})
public class VPurchaseArrivalMainController extends BaseController {

    @Autowired
    private IVPurchaseArrivalMainService arrivalMainService;
    @Autowired
    private IVPurchaseInvoiceMainService invoiceMainService;

    @PostMapping("save")
    @ApiOperation("保存到货单")
    public HttpResult save(@RequestBody VPurchaseArrivalMain purchaseArrivalMain) {
        if(purchaseArrivalMain.getOrderDetailList().size() == 0) {
            throw new NuoNiuException("明细不能为空");
        }

        if(purchaseArrivalMain.getId() != null) {
            List<VPurchaseInvoiceMain> purchaseInvoiceMains = invoiceMainService.getBySourceCode(purchaseArrivalMain.getVoucherCode());
            if(purchaseInvoiceMains.size() > 0) {
                throw new NuoNiuException("已生成发票,不能修改");
            }
        }

        arrivalMainService.saveOrder(purchaseArrivalMain);
        return HttpResult.ok();
    }

    @DeleteMapping("delete")
    @ApiOperation("删除采购到货单")
    public HttpResult delete(Long id) {
        VPurchaseArrivalMain purchaseArrivalMain = arrivalMainService.getById(id);
        List<VPurchaseInvoiceMain> purchaseInvoiceMains = invoiceMainService.getBySourceCode(purchaseArrivalMain.getVoucherCode());
        if(purchaseInvoiceMains.size() > 0) {
            throw new NuoNiuException("已生成发票,不能删除");
        }

        arrivalMainService.deleteById(id);
        return HttpResult.ok();
    }

    @GetMapping("detail")
    @ApiModelProperty("获取单据详情")
    public HttpResult detail(Integer id) {
        return HttpResult.ok(arrivalMainService.getById(id));
    }

    /**
     * 提交
     */
    @GetMapping("submit")
    @ApiOperation("单据提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:提交,-1:反提交")
    })
    public HttpResult submit(Long id, Integer status) {
        arrivalMainService.submit(id, status);
        return HttpResult.ok();
    }

    /**
     * 到货单审核/弃审
     * @author 小啊刚
     * @date 2020-11-25
     */
    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        arrivalMainService.examine(id, status);
        return HttpResult.ok();
    }

    /**
     * 获取单据编码
     * @author 小啊刚
     * @date 2020-11-27
     */
    @GetMapping("getCode")
    @ApiOperation("获取单据编码")
    public HttpResult getCode() {
        return HttpResult.ok(arrivalMainService.getCode(UserThreadLocal.get().getCompanyId()));
    }

    /*******************   委外到货单 **********************/
    /**
     * 获取 委外到货单 编码
     */
    @GetMapping("getCodeForDelegate")
    @ApiOperation("获取 委外到货单 编码")
    public HttpResult getBySourceCode() {
        return HttpResult.ok(arrivalMainService.getCodeForDelegate());
    }


    @PostMapping("saveOrderForDelegate")
    @ApiOperation("保存 委外到货单")
    public HttpResult saveOrderForDelegate(@RequestBody VPurchaseArrivalMain purchaseArrivalMain) {
        arrivalMainService.saveOrderForDelegate(purchaseArrivalMain);
        return HttpResult.ok();
    }

    @DeleteMapping("deleteByIdForDelegate")
    @ApiOperation("删除 委外到货单")
    public HttpResult deleteByIdForDelegate(Long id) {
        arrivalMainService.deleteByIdForDelegate(id);
        return HttpResult.ok();
    }

}
